﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelAddIn2010.ExcelOp
{
    public partial class FrmDeleteWksht : Form
    {
        Excel.Workbook m_wkBk;
        string m_msgTitle;
        public FrmDeleteWksht()
        {
            InitializeComponent();
        }
        private void InitCkList()
        {
            this.cklstWorksheets.Items.Clear();
            foreach (Excel.Worksheet wkSht in m_wkBk.Worksheets)
            {
                this.cklstWorksheets.Items.Add(wkSht.Name);
            }
        }
        private void FrmDeleteWksht_Load(object sender, EventArgs e)
        {
            m_wkBk = gValue.ExcelApp.ActiveWorkbook;
            m_msgTitle = "删除工作表";
            InitCkList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (m_wkBk.ProtectStructure && m_wkBk.ProtectWindows)
            {
                MessageBox.Show("工作簿受保护，不能删除工作表，谢谢", m_msgTitle, MessageBoxButtons.OK);
                return;
            }
            if (this.cklstWorksheets.CheckedItems.Count == this.cklstWorksheets.Items.Count)
            {
                if (1 == this.cklstWorksheets.Items.Count)
                {
                    MessageBox.Show("该工作簿只有一个工作表，不能删除，谢谢", m_msgTitle, MessageBoxButtons.OK);
                }
                else
                {
                    MessageBox.Show("不能删除全部工作表，至少保留一个工作表，谢谢", m_msgTitle, MessageBoxButtons.OK);
                }
                return;
            }
            if (0 == this.cklstWorksheets.CheckedItems.Count)
            {
                MessageBox.Show("请勾选一个工作表，谢谢", m_msgTitle, MessageBoxButtons.OK);
                return;
            }
            gValue.ExcelApp.DisplayAlerts = false;
            foreach (object objCheckedItem in this.cklstWorksheets.CheckedItems)
            {
                m_wkBk.Worksheets[objCheckedItem.ToString()].Delete();
            }
            gValue.ExcelApp.DisplayAlerts = true;
            InitCkList();

        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int idx = 0; idx < this.cklstWorksheets.Items.Count;idx++ )
            {
                this.cklstWorksheets.SetItemChecked(idx, true);
            }
        }

        private void btnSelectReverse_Click(object sender, EventArgs e)
        {
            for (int idx = 0; idx < this.cklstWorksheets.Items.Count; idx++)
            {
                this.cklstWorksheets.SetItemChecked(idx, !this.cklstWorksheets.GetItemChecked(idx));
            }
        }

        private void btnSetActive_Click(object sender, EventArgs e)
        {
            if (0 == this.cklstWorksheets.CheckedItems.Count)
            {
                MessageBox.Show("请勾选一个工作表，谢谢", m_msgTitle, MessageBoxButtons.OK);
                return;
            }

            if (this.cklstWorksheets.CheckedItems.Count > 1)
            {
                MessageBox.Show("只能设置一个工作表为活动工作表，谢谢", m_msgTitle, MessageBoxButtons.OK);
                return;
            }
            foreach (object objCheckedItem in this.cklstWorksheets.CheckedItems)
            {
                m_wkBk.Worksheets[objCheckedItem.ToString()].Select();
            }
        }
    }
}
